package com.umerclass.ui.anmo.three;

import android.content.DialogInterface;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.umerclass.MainActivity;
import com.umerclass.R;
import com.umerclass.bean.anmo.YeJiBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.okhttp.OkEntityListRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.frame.ui.recycleview.PullLoadMoreRecyclerView;
import com.umerclass.frame.ui.recycleview.listener.DataStateListener;
import com.umerclass.frame.ui.recycleview.listener.OptListener;
import com.umerclass.frame.ui.statelayout.StateLayout;
import com.umerclass.ui.anmo.three.adapter.YeJiAdapter;
import com.umerclass.utils.Constants;
import com.umerclass.utils.ItemClickSupport;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

public class MyYeJiActivity extends BaseActivity implements DataStateListener,
        PullLoadMoreRecyclerView.PullLoadMoreListener, OptListener ,
        DatePickerDialog.OnDateSetListener,
        TimePickerDialog.OnTimeSetListener  {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_date_one)
    TextView tvDateOne;
    @BindView(R.id.tv_date_two)
    TextView tvDateTwo;
    @BindView(R.id.tv_select)
    TextView tvSelect;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.pullLoadMoreRecyclerView)
    PullLoadMoreRecyclerView pullLoadMoreRecyclerView;
    @BindView(R.id.state_layout)
    StateLayout stateLayout;
    private int operation = 1;//入账：0；出账：1
    private String startDayTime = "";
    private String endDayTime = "";
    private List<YeJiBean> yeJiBeanList = new ArrayList<>();
    private YeJiAdapter yeJiAdapter;

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_anmo_shop_yeji;
    }

    @Override
    public void init() {
        initView();
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back,R.id.tv_date_one,R.id.tv_date_two})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv_date_one:
                Datepicker();
                break;
            case R.id.tv_date_two:
                Datepicker();
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {

    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.myPerformanceList:
                yeJiBeanList = (List<YeJiBean>) t.getT();
                yeJiAdapter.addDataSource(yeJiBeanList, t);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.myPerformanceList:
                swipeRefreshLayout.setRefreshing(false);
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh() {
        yeJiAdapter.reset();
        loadData();
    }

    @Override
    public void onLoadMore() {
        loadData();
    }

    @Override
    public void onDataState(int flag) {

    }

    private void getDatas(){
        OkEntityListRequest<YeJiBean> request = new OkEntityListRequest<>(R.id.myPerformanceList,
                Constants.myPerformanceList, YeJiBean.class);
        request.addParams("userId", "");
        request.addParams("operation", operation);
        request.addParams("startDayTime", startDayTime);
        request.addParams("endDayTime", endDayTime);
        request.addParams("page",yeJiAdapter.getPage());
        request.addParams("pageSize",10);
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    private void initView() {

        stateLayout.setRefreshListener(new StateLayout.OnViewRefreshListener() {
            @Override
            public void refreshClick() {
                loadData();
            }

            @Override
            public void loginClick() {
            }
        });
        pullLoadMoreRecyclerView.init(recyclerView, swipeRefreshLayout);
        yeJiAdapter = new YeJiAdapter(this, R.layout.fragment_team_item, pullLoadMoreRecyclerView,
                this,this);
        pullLoadMoreRecyclerView.setAdapterTwo(yeJiAdapter);
        pullLoadMoreRecyclerView.setOnPullLoadMoreListener(this);
        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorAccent, R.color.colorAccent);
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法

        //单击
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView())
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                    }
                });
        //长按
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView()).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
                return false;
            }
        });
    }

    FragmentManager fragmentManager;
    DatePickerDialog datePickerDialog ;
    TimePickerDialog timePickerDialog ;
    int Year, Month, Day, Hour, Minute;
    Calendar calendar ;

    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
        String date = "Date: "+year+"-"+monthOfYear+1+"-"+dayOfMonth;
        Toast.makeText(MyYeJiActivity.this, date, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {

    }
    public void Datepicker() {
        datePickerDialog = DatePickerDialog.newInstance(this, Year, Month, Day);
        datePickerDialog.setThemeDark(false);
        datePickerDialog.showYearPickerFirst(false);
        datePickerDialog.setTitle("Date Picker");
        fragmentManager =getSupportFragmentManager();
        datePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialogInterface) {
                Toast.makeText(MyYeJiActivity.this, "Datepicker Canceled", Toast.LENGTH_SHORT).show();
            }
        });
        datePickerDialog.show(fragmentManager, "DatePickerDialog");
    }
}
